<template>
  <div>
    <el-dialog
      :title="obj.isadd?'菜单添加':'菜单编辑'"
      :visible.sync="obj.istrue"
      width="50%"
      :before-close="quxiao"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="form.pid" placeholder="请选择上级菜单" @change=changepid()>
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in list" :key='item.id' :label="item.title" :value="item.id"></el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型">
          <el-radio :label="1" v-model="form.type" disabled>目录</el-radio>
          <el-radio :label="2" v-model="form.type" disabled>菜单</el-radio>
          <!-- {{ form.type }} -->
        </el-form-item>
        <el-form-item label="菜单图标" v-show="form.type == 1">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option
              v-for="(item, index) in iconarr"
              :key="index"
              :value="item"
            >
              <i :class="item"></i>
            </el-option>
          </el-select>
          <!-- {{ form.icon }} -->
        </el-form-item>
        <el-form-item label="菜单地址" v-show="form.type == 2">
          <el-select v-model="form.url" placeholder="请选择菜单地址">
            <el-option
              v-for="(item, index) in twopath"
              :key="index"
              :value="'/' + item.path"
              :label="item.meta.title"
            >
            </el-option>
          </el-select>
          <!-- {{ form.url }} -->
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
          <!-- {{ form.status }} -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao()">取 消</el-button>
        <el-button type="primary" @click='submit()' v-if='obj.isadd'>添 加</el-button>
        <el-button type="primary" @click="bianji()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { twopath } from "../../../router/index.js";
import { mapActions, mapGetters } from "vuex";
import {menuadd,menuinfo,menuedit} from '../../../request/api'
import {successAlert,warningAlert} from '../../../util/alert'
export default {
  props: ["obj"],
  data() {
    return {
      istrue: true,
      form: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      iconarr: [
        "el-icon-eleme",
        "el-icon-user",
        "el-icon-phone",
        "el-icon-s-goods",
        "el-icon-help",
        "el-icon-upload",
      ],
      twopath: twopath,
    };
  },
  methods: {
    //   
    changepid(){
        if(this.form.pid==0){
            this.form.type=1
            this.form.path=''
        }else{
            this.form.type=2
            this.form.icon=''
        }
    },
    // 封装清空
    empty() {
      this.form = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    //点击取消
    quxiao() {
      this.obj.istrue = false;
      this.empty()
    },
    ...mapActions({
      reqchangelist: "menu/reqchangelist",
    }),
    // 点击添加
    submit(){
        menuadd(this.form).then(res=>{
            // console.log(res);
            if(res.data.code==200){
                successAlert(res.data.msg)
                // 关闭弹框清空内容
                this.quxiao()
                // 刷新列表
                this.reqchangelist()
            }else{
                warningAlert(res.data.msg)
            }
        })
    },
    // 获取一条数据渲染上去
    getinfo(id){
        menuinfo({id:id}).then(res=>{
            this.form=res.data.list,
            this.form.id=id
            console.log(this.form);
        })
    },
    // 点击编辑
    bianji(){
        menuedit(this.form).then(res=>{
            if(res.data.code==200){
                successAlert(res.data.msg)
                // 刷新数据
                this.reqchangelist()
                // 关闭弹框清空内容
                this.quxiao()
            }else{
                warningAlert(res.data.msg)
            }
        })
    }
  },
  computed: {
    ...mapGetters({
      list: "menu/list",
    }),
  },
  mounted() {
    this.reqchangelist();
  },
};
</script>

<style lang="less" scoped>
</style>